home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1993 July / Internet Tools.iso / RockRidge / mail / elm / elm2.3pl11 / utils / answer.c next >
Encoding:
C/C++ Source or Header  |  1990-04-28  |  9.7 KB  |  409 lines

  1.  
  2. static char rcsid[] = "@(#)$Id: answer.c,v 4.1 90/04/28 22:44:27 syd Exp $";
  3.  
  4. /*******************************************************************************
  5.  *  The Elm Mail System  -  $Revision: 4.1 $   $State: Exp $
  6.  *
  7.  *             Copyright (c) 1986, 1987 Dave Taylor
  8.  *             Copyright (c) 1988, 1989, 1990 USENET Community Trust
  9.  *******************************************************************************
  10.  * Bug reports, patches, comments, suggestions should be sent to:
  11.  *
  12.  *    Syd Weinstein, Elm Coordinator
  13.  *    elm@DSI.COM            dsinc!elm
  14.  *
  15.  *******************************************************************************
  16.  * $Log:    answer.c,v $
  17.  * Revision 4.1  90/04/28  22:44:27  syd
  18.  * checkin of Elm 2.3 as of Release PL0
  19.  * 
  20.  *
  21.  ******************************************************************************/
  22.  
  23. /** This program is a phone message transcription system, and
  24.     is designed for secretaries and the like, to allow them to
  25.     painlessly generate electronic mail instead of paper forms.
  26.  
  27.     Note: this program ONLY uses the local alias file, and does not
  28.       even read in the system alias file at all.
  29.  
  30. **/
  31.  
  32. #include <stdio.h>
  33. #include <fcntl.h>
  34. #include <ctype.h>
  35.  
  36. #include "defs.h"            /* ELM system definitions      */
  37.  
  38. #define  ELM        "elm"        /* where the elm program lives */
  39.  
  40. #define  answer_temp_file    "/tmp/answer."
  41.  
  42. static char ident[] = { WHAT_STRING };
  43.  
  44. struct alias_rec user_hash_table  [MAX_UALIASES];
  45.  
  46. int user_data;        /* fileno of user data file   */
  47.  
  48. char *expand_group(), *get_alias_address(), *get_token(), *strip_parens(),
  49.     *shift_lower();
  50.  
  51. main()
  52. {
  53.     FILE *fd;
  54.     char *address, buffer[LONG_STRING], tempfile[SLEN], *cp;
  55.     char  name[SLEN], user_name[SLEN];
  56.     int   msgnum = 0, eof;
  57.     
  58.     read_alias_files();
  59.  
  60.     while (1) {
  61.       if (msgnum > 9999) msgnum = 0;
  62.     
  63.       printf("\n-------------------------------------------------------------------------------\n");
  64.  
  65. prompt:   printf("\nMessage to: ");
  66.       if (fgets(user_name, SLEN, stdin) == NULL) {
  67.         putchar('\n');
  68.         exit(0);
  69.       }
  70.       if(user_name[0] == '\0')
  71.         goto prompt;
  72.       
  73.       cp = &user_name[strlen(user_name)-1];
  74.       if(*cp == '\n') *cp = '\0';
  75.       if(user_name[0] == '\0')
  76.         goto prompt;
  77.  
  78.       if ((strcmp(user_name,"quit") == 0) ||
  79.           (strcmp(user_name,"exit") == 0) ||
  80.           (strcmp(user_name,"done") == 0) ||
  81.           (strcmp(user_name,"bye")  == 0))
  82.          exit(0);
  83.  
  84.       if (translate(user_name, name) == 0)
  85.         goto prompt;
  86.  
  87.       address = get_alias_address(name, 1, 0);
  88.  
  89.       printf("address '%s'\n", address);
  90.  
  91.       if (address == NULL || strlen(address) == 0) {
  92.         printf("Sorry, could not find '%s' [%s] in list!\n", user_name, 
  93.            name);
  94.         goto prompt;
  95.       }
  96.  
  97.       sprintf(tempfile, "%s%d", answer_temp_file, msgnum++);
  98.  
  99.       if ((fd = fopen(tempfile,"w")) == NULL)
  100.         exit(printf("** Fatal Error: could not open %s to write\n",
  101.          tempfile));
  102.  
  103.  
  104.       printf("\nEnter message for %s ending with a blank line.\n\n", 
  105.          user_name);
  106.  
  107.       fprintf(fd,"\n\n");
  108.  
  109.       do {
  110.        printf("> ");
  111.        if (! (eof = (fgets(buffer, SLEN, stdin) == NULL))) 
  112.          fprintf(fd, "%s", buffer);
  113.       } while (! eof && strlen(buffer) > 1);
  114.     
  115.       fclose(fd);
  116.  
  117.       sprintf(buffer, 
  118.          "((%s -s \"While You Were Out\" %s ; %s %s) & ) < %s > /dev/null",
  119.          ELM, strip_parens(address), remove_cmd, tempfile, tempfile);
  120.  
  121.       system(buffer);
  122.     }
  123. }
  124.  
  125. int
  126. translate(fullname, name)
  127. char *fullname, *name;
  128. {
  129.     /** translate fullname into name..
  130.            'first last'  translated to first_initial - underline - last
  131.            'initial last' translated to initial - underline - last
  132.         Return 0 if error.
  133.     **/
  134.     register int i, lastname = 0, len;
  135.  
  136.     for (i=0, len = strlen(fullname); i < len; i++) {
  137.  
  138.       if (isupper(fullname[i]))
  139.          fullname[i] = tolower(fullname[i]);
  140.  
  141.       if (fullname[i] == ' ') 
  142.         if (lastname) {
  143.           printf(
  144.           "** Can't have more than 'FirstName LastName' as address!\n");
  145.           return(0);
  146.         }
  147.         else
  148.           lastname = i+1;
  149.     
  150.     }
  151.  
  152.     if (lastname) 
  153.       sprintf(name, "%c_%s", fullname[0], (char *) fullname + lastname);
  154.     else
  155.       strcpy(name, fullname);
  156.  
  157.     return(1);
  158. }
  159.  
  160.         
  161. read_alias_files()
  162. {
  163.     /** read the user alias file **/
  164.  
  165.     char fname[SLEN];
  166.     int  hash;
  167.  
  168.     sprintf(fname,  "%s/.elm/aliases.hash", getenv("HOME")); 
  169.  
  170.     if ((hash = open(fname, O_RDONLY)) == -1) 
  171.       exit(printf("** Fatal Error: Could not open %s!\n", fname));
  172.  
  173.     read(hash, user_hash_table, sizeof user_hash_table);
  174.     close(hash);
  175.  
  176.     sprintf(fname,  "%s/.elm/aliases.data", getenv("HOME")); 
  177.  
  178.     if ((user_data = open(fname, O_RDONLY)) == -1) 
  179.       return;
  180. }
  181.  
  182. char *get_alias_address(name, mailing, depth)
  183. char *name;
  184. int   mailing, depth;
  185. {
  186.     /** return the line from either datafile that corresponds 
  187.         to the specified name.  If 'mailing' specified, then
  188.         fully expand group names.  Returns NULL if not found.
  189.         Depth is the nesting depth, and varies according to the
  190.         nesting level of the routine.  **/
  191.  
  192.     static char buffer[VERY_LONG_STRING];
  193.     int    loc;
  194.  
  195.     name = shift_lower(name);
  196.     if ((loc = find(name, user_hash_table, MAX_UALIASES)) >= 0) {
  197.       lseek(user_data, ntohl(user_hash_table[loc].byte), 0L);
  198.       get_line(user_data, buffer);
  199.       if (buffer[0] == '!' && mailing)
  200.         return( (char *) expand_group(buffer, depth));
  201.       else
  202.         return( (char *) buffer);
  203.     }
  204.     
  205.     return( (char *) NULL);
  206. }
  207.  
  208. char *expand_group(members, depth)
  209. char *members;
  210. int   depth;
  211. {
  212.     /** given a group of names separated by commas, this routine
  213.         will return a string that is the full addresses of each
  214.         member separated by spaces.  Depth is the current recursion
  215.         depth of the expansion (for the 'get_token' routine) **/
  216.  
  217.     char   buffer[VERY_LONG_STRING];
  218.     char   buf[LONG_STRING], *word, *address, *bufptr;
  219.  
  220.     strcpy(buf, members);     /* parameter safety! */
  221.     buffer[0] = '\0';    /* nothing in yet!   */
  222.     bufptr = (char *) buf;    /* grab the address  */
  223.     depth++;        /* one more deeply into stack */
  224.  
  225.     while ((word = (char *) get_token(bufptr, "!, ", depth)) != NULL) {
  226.       if ((address = (char *) get_alias_address(word, 1, depth)) == NULL) {
  227.         fprintf(stderr, "Alias %s not found for group expansion!", word);
  228.         return( (char *) NULL);
  229.       }
  230.       else if (strcmp(buffer,address) != 0) {
  231.         sprintf(buffer,"%s %s", buffer, address);
  232.       }
  233.  
  234.       bufptr = NULL;
  235.     }
  236.  
  237.     return( (char *) buffer);
  238. }
  239.  
  240. int
  241. find(word, table, size)
  242. char *word;
  243. struct alias_rec table[];
  244. int size;
  245. {
  246.     /** find word and return loc, or -1 **/
  247.     register int loc;
  248.     
  249.     if (strlen(word) > 20)
  250.       exit(printf("Bad alias name: %s.  Too long.\n", word));
  251.  
  252.     loc = hash_it(word, size);
  253.  
  254.     while (strcmp(word, table[loc].name) != 0) {
  255.       if (table[loc].name[0] == '\0') 
  256.         return(-1);
  257.       loc = (loc + 1) % size; 
  258.     }
  259.  
  260.     return(loc);
  261. }
  262.  
  263. int
  264. hash_it(string, table_size)
  265. char *string;
  266. int   table_size;
  267. {
  268.     /** compute the hash function of the string, returning
  269.         it (mod table_size) **/
  270.  
  271.     register int i, sum = 0;
  272.     
  273.     for (i=0; string[i] != '\0'; i++)
  274.       sum += (int) string[i];
  275.  
  276.     return(sum % table_size);
  277. }
  278.  
  279. get_line(fd, buffer)
  280. int fd;
  281. char *buffer;
  282. {
  283.     /* read from file fd.  End read upon reading either 
  284.        EOF or '\n' character (this is where it differs 
  285.        from a straight 'read' command!) */
  286.  
  287.     register int i= 0;
  288.     char     ch;
  289.  
  290.     while (read(fd, &ch, 1) > 0)
  291.       if (ch == '\n' || ch == '\r') {
  292.         buffer[i] = 0;
  293.         return;
  294.       }
  295.       else
  296.         buffer[i++] = ch;
  297. }
  298.  
  299. print_long(buffer, init_len)
  300. char *buffer;
  301. int   init_len;
  302. {
  303.     /** print buffer out, 80 characters (or less) per line, for
  304.         as many lines as needed.  If 'init_len' is specified, 
  305.         it is the length that the first line can be.
  306.     **/
  307.  
  308.     register int i, loc=0, space, length, len; 
  309.  
  310.     /* In general, go to 80 characters beyond current character
  311.        being processed, and then work backwards until space found! */
  312.  
  313.     length = init_len;
  314.  
  315.     do {
  316.       if (strlen(buffer) > loc + length) {
  317.         space = loc + length;
  318.         while (buffer[space] != ' ' && space > loc + 50) space--;
  319.         for (i=loc;i <= space;i++)
  320.           putchar(buffer[i]);
  321.         putchar('\n');
  322.         loc = space;
  323.       }
  324.       else {
  325.         for (i=loc, len = strlen(buffer);i < len;i++)
  326.           putchar(buffer[i]);
  327.         putchar('\n');
  328.         loc = len;
  329.       }
  330.       length = 80;
  331.     } while (loc < strlen(buffer));
  332. }
  333.  
  334. /****
  335.      The following is a newly chopped version of the 'strtok' routine
  336.   that can work in a recursive way (up to 20 levels of recursion) by
  337.   changing the character buffer to an array of character buffers....
  338. ****/
  339.  
  340. #define MAX_RECURSION        20        /* up to 20 deep recursion */
  341.  
  342. #undef  NULL
  343. #define NULL            (char *) 0    /* for this routine only   */
  344.  
  345. extern char *strpbrk();
  346.  
  347. char *get_token(string, sepset, depth)
  348. char *string, *sepset;
  349. int  depth;
  350. {
  351.  
  352.     /** string is the string pointer to break up, sepstr are the
  353.         list of characters that can break the line up and depth
  354.         is the current nesting/recursion depth of the call **/
  355.  
  356.     register char    *p, *q, *r;
  357.     static char    *savept[MAX_RECURSION];
  358.  
  359.     /** is there space on the recursion stack? **/
  360.  
  361.     if (depth >= MAX_RECURSION) {
  362.      fprintf(stderr,"Error: Get_token calls nested greated than %d deep!\n",
  363.             MAX_RECURSION);
  364.      exit(1);
  365.     }
  366.  
  367.     /* set up the pointer for the first or subsequent call */
  368.     p = (string == NULL)? savept[depth]: string;
  369.  
  370.     if(p == 0)        /* return if no tokens remaining */
  371.         return(NULL);
  372.  
  373.     q = p + strspn(p, sepset);    /* skip leading separators */
  374.  
  375.     if (*q == '\0')        /* return if no tokens remaining */
  376.         return(NULL);
  377.  
  378.     if ((r = strpbrk(q, sepset)) == NULL)    /* move past token */
  379.         savept[depth] = 0;    /* indicate this is last token */
  380.     else {
  381.         *r = '\0';
  382.         savept[depth] = ++r;
  383.     }
  384.     return(q);
  385. }
  386.  
  387. char *strip_parens(string)
  388. char *string;
  389. {
  390.     /** Return string with all parenthesized information removed.
  391.         This is a non-destructive algorithm... **/
  392.  
  393.     static char  buffer[LONG_STRING];
  394.     register int depth = 0, buffer_index = 0;
  395.  
  396.     for (; *string; string++) {
  397.       if (*string == '(')
  398.         depth++;
  399.       else if (*string == ')') 
  400.         depth--;
  401.       else if (depth == 0)
  402.         buffer[buffer_index++] = *string;
  403.     }
  404.     
  405.     buffer[buffer_index] = '\0';
  406.  
  407.     return( (char *) buffer);
  408. }
  409.